home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / pickfile.pro < prev    next >
Text File  |  1997-07-08  |  9KB  |  226 lines

  1. ; $Id: pickfile.pro,v 1.20 1997/04/17 19:50:29 lubos Exp $
  2. ;
  3. ; Copyright (c) 1991-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       PICKFILE
  8. ;
  9. ; PURPOSE:
  10. ;       This function allows the user to interactively pick a file.  A file
  11. ;       selection tool with a graphical user interface is created.  Files
  12. ;       can be selected from the current directory or other directories.
  13. ;
  14. ; CATEGORY:
  15. ;       Widgets.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;       Result = PICKFILE()
  19. ;
  20. ; KEYWORD PARAMETERS:
  21. ;
  22. ;       FILE:   A string value for setting the initial value of the
  23. ;               selection. Useful if there is a default file
  24. ;
  25. ;       GET_PATH: Set to a named variable. Returns the path at the
  26. ;               time of selection.
  27. ;
  28. ;       GROUP:  The widget ID of the widget that calls PICKFILE.  When this
  29. ;               ID is specified, a death of the caller results in the death of
  30. ;               the PICKFILE widget application.
  31. ;
  32. ;       READ:   Set this keyword to make the title of the PICKFILE window
  33. ;               "Select File to Read".
  34. ;
  35. ;       WRITE:  Set this keyword to make the title of the PICKFILE window
  36. ;               "Select File to Write".
  37. ;
  38. ;       PATH:   The initial path to select files from.  If this keyword is
  39. ;               not set, the current directory is used.
  40. ;
  41. ;       FILTER: A string value for filtering the files in the file list.  This
  42. ;               keyword is used to reduce the number of files to choose from.
  43. ;               The user can modify the filter unless the FIX_FILTER keyword
  44. ;               is set.  Example filter values might be "*.pro" or "*.dat".
  45. ;
  46. ;       FIX_FILTER: When this keyword is set, only files that satisfy the
  47. ;               filter can be selected.  The user has no ability to modify
  48. ;               the filter and the filter is not shown.
  49. ;
  50. ;       TITLE:  A scalar string to be used for the window title.  If it is
  51. ;               not specified, the default title is "Select File"
  52. ;
  53. ;       NOCONFIRM: Return immediately upon selection of a file.  The default
  54. ;               behavior is to display the selection and then return the
  55. ;               file when the user uses the "ok" button.
  56. ;
  57. ;       MUST_EXIST: When set, only files that actually exist can be selected.
  58. ;
  59. ; OUTPUTS:
  60. ;       PICKFILE returns a string that contains the name of the file selected.
  61. ;       If no file is selected, PICKFILE returns a null string.
  62. ;
  63. ; COMMON BLOCKS:
  64. ;       NONE.
  65. ;
  66. ; SIDE EFFECTS:
  67. ;       NONE.
  68. ;
  69. ; RESTRICTIONS:
  70. ;       Only one instance of the PICKFILE widget can be running at one time.
  71. ;
  72. ; PROCEDURE:
  73. ;       Create and register the widget and then exit, returning the filename
  74. ;       that was picked.
  75. ;
  76. ; EXAMPLE:
  77. ;       Create a PICKFILE widget that lets users select only files with
  78. ;       the extensions 'pro' and 'dat'.  Use the 'Select File to Read' title
  79. ;       and store the name of the selected file in the variable F.  Enter:
  80. ;
  81. ;               F = PICKFILE(/READ, FILTER = '*.pro *.dat')
  82. ;
  83. ; MODIFICATION HISTORY:
  84. ;       Written by:     Steve Richards, April, 1991
  85. ;       July, 1991      Added a FILTER keyword to allow users
  86. ;                       to select files with a given extension or
  87. ;                       extensions.
  88. ;       August, 1991    Fixed bugs caused by differences between
  89. ;                       spawned ls commands on different machines.
  90. ;       September, 1991 Made Myfindfile so only one pass was
  91. ;                       necessary to find files and directories.
  92. ;       3/92 - ACY      Corrected initialization of dirsave, change spawn
  93. ;                       command to "ls -lL" and added case for links
  94. ;                       add NOCONFIRM keyword for auto exiting on selection
  95. ;       8/92 - SMR      Rewrote pickfile as a compound widget.
  96. ;       10/92 - SMR     Fixed a bug where extremely large file namess didn't
  97. ;                       show up properly in the file list or as return
  98. ;                       values.
  99. ;       12/92 - JWG     Add better machine dependency code
  100. ;       1/93 - JWG      Added FILE, GET_PATH keywords.
  101. ;       1/93 - TAC      Added Windows Common dialog pickfile code
  102. ;       2/93 - SMR      Fixed the documentation example for multiple extensions
  103. ;       1/94 - KDB      If directory had no execute permission on Unix
  104. ;                       platforms, CD fails and causes error. Added check
  105. ;                       for this. Increased spawn speed by using /sh for unix.
  106. ;                       Added -a switch to ls so that all files can be found
  107. ;                       on unix machines.
  108. ;       2/94 - KDB    Values passed to CD cannot end in a '\' on DOS
  109. ;            platforms. Program would crash if the PATH keyword
  110. ;            was supplied a value that ended with a "\". Added
  111. ;                a check for this.
  112. ;    3/94 - BMH    Deleted the reference here to OS_PICKFILE for the
  113. ;            Unix platforms and created an IDL routine to
  114. ;            to call the Mac and Windows specific OS_PICKFILE
  115. ;            routines.  This solved the saving and restoring on
  116. ;             different platforms problem.
  117. ;    4/94 - KDB      The vms call to lib$findfile in valid_dir was
  118. ;                commented out. This caused errors when path was
  119. ;            changed by user. Uncommented. In Valid_Dir, with
  120. ;            vms the type of directory specification was not
  121. ;                checked (directory can be a path or a filename):
  122. ;            Fixed this. In dirlist section of event handler,
  123. ;                a "[-]" would get trimmed to "" and cause error:
  124. ;            Fixed.
  125. ;    8/94 - ACY      Change the spawn command in getdirs to send error
  126. ;            output to /dev/null.
  127. ;    12/94 - DJE    Fix the FIX_FILTER option for the MacOS.
  128. ;    1/96 - RPM    Fixed reading of directories for when Unix long listing
  129. ;            (ls -l) does not align columns.
  130. ;    3/96 - LP    Implemented widget_pickfile in Motif
  131. ;                       (xmFileSelectionBox). Conformed to widget_...
  132. ;                       mechanism for all platforms.
  133. ;            Used Motif widget_pickfile, removed file, directory
  134. ;            internal IDL handling.
  135. ;    4/96 - LP    Renamed widget_pickfile to dialog_pickfile.
  136. ;
  137. ; Note. This routine is maintained for compatibility reason. New system
  138. ;       routine dialog_pickfile should be used instead.
  139. ;-
  140. ;
  141.  
  142. ;------------------------------------------------------------------------------
  143. ;       procedure Pickfile
  144. ;------------------------------------------------------------------------------
  145. FUNCTION Pickfile, Group=GROUP, Path=PATH, Read=READ, Write=WRITE, $
  146.                    Filter=FILTER, Title=TITLE, Noconfirm=NOCONFIRM, $
  147.                    Must_exist=MUST_EXIST, Fix_filter=FIX_FILTER, $
  148.                    FILE=FILE, GET_PATH=GET_PATH
  149.     
  150.     here = ""
  151.     thefile = ""
  152.     filt = ""
  153.     IF (KEYWORD_SET(NOCONFIRM))     THEN auto_exit = 1      ELSE auto_exit = 0
  154.     IF (KEYWORD_SET(MUST_EXIST))    THEN existflag = 1      ELSE existflag = 0
  155.     IF (KEYWORD_SET(FIX_FILTER))    THEN mapfilter = 1      ELSE mapfilter = 0
  156.     IF (KEYWORD_SET(READ))          THEN readflag  = 1      ELSE readflag  = 0
  157.     IF (KEYWORD_SET(WRITE))         THEN writeflag = 1      ELSE writeflag = 0
  158.     IF (NOT (KEYWORD_SET(TITLE)))   THEN TITLE = ""
  159.     IF (N_ELEMENTS(FILE) EQ 0)      THEN FILE = ""
  160.     IF (N_ELEMENTS(GROUP) EQ 0)     THEN GROUP = 0
  161.     
  162.     CASE !Version.os OF
  163.         'vms': begin
  164.             separator       = ''
  165.             IF (KEYWORD_SET(FILTER)) THEN filt = FILTER[0]
  166.         end
  167. ; WINDOWS does NOT want a \ at the end of the directory
  168.         'Win32': begin
  169.             separator = ''
  170.             IF (KEYWORD_SET(FILTER)) THEN filt = FILTER[0] ELSE filt = "*.*"
  171.         end
  172.         'MacOS': begin
  173.             separator = ""
  174.             IF (KEYWORD_SET(FILTER)) THEN filt = FILTER[0] ELSE filt = "*"
  175.         end
  176.         ELSE: begin
  177.             separator       = '/'
  178.             IF (KEYWORD_SET(FILTER)) THEN filt = FILTER[0]
  179.             end
  180.     ENDCASE
  181.     
  182.     CD, CURRENT = dirsave
  183.     
  184.     IF (N_ELEMENTS(PATH) EQ 0) THEN BEGIN
  185.         
  186.         PATH = dirsave + separator
  187.         here = PATH
  188.         
  189.     ENDIF ELSE BEGIN
  190.         
  191.         ;; When on a Dos platform the argument to CD cannot end in a '\' unless
  192.         ;; it is a root directory of a drive (ie C:\). Because of this, check
  193.         ;; If we must remove the last character of PATH. -KDB 2/4/94
  194.         
  195.         IF((!Version.os EQ 'Win32')AND  $
  196.            (Strpos(path, '\', Strlen(PATH)-1)NE -1))THEN  BEGIN
  197.             IF(strlen(path) GT 3)THEN  $ ; Root dirs are 3 chars long.
  198.               path = Strmid( path, 0, Strlen(path)-1)
  199.         ENDIF
  200.         
  201.         IF(STRPOS(PATH, separator, STRLEN(PATH)- 1) EQ -1) AND $
  202.           (PATH NE separator)THEN $
  203.           PATH = PATH + separator
  204.         
  205.         CATCH, errorStatus
  206.         if errorStatus NE 0 then begin
  207.             here = ""
  208.         endif else begin
  209.             CD, PATH            ;if the user selected
  210.             CATCH, /CANCEL
  211.             here = PATH         ;a path then use it
  212.         endelse
  213.     ENDELSE
  214.     
  215.     thefile = DIALOG_PICKFILE(GROUP = GROUP, FILTER = filt, TITLE = TITLE, $
  216.                               MUST_EXIST = existflag, FILE = FILE, $
  217.                               NOCONFIRM = auto_exit, FIX_FILTER = mapfilter, $
  218.                               GET_PATH = here, PATH = PATH, $
  219.                               READ = readflag, WRITE = writeflag)
  220.     
  221.     CD, dirsave
  222.     GET_PATH = here
  223.     RETURN, thefile
  224. END ;====================== end of Pickfile routine ===========================
  225.  
  226.